package main

import (
	"fmt"
	"sync"
	"time"
)

type SingleObject struct{}

// 此对象的Do方法只会执行一次
// Once内部也是以来Mutex锁实现的单词执行
var one sync.Once

var instance *SingleObject

func getInstance() *SingleObject {
	//指挥执行一次
	one.Do(func() {
		instance = &SingleObject{}
		fmt.Println("实例化SingleObject")
	})
	return instance
}

func main() {
	for i := 0; i < 100; i++ {
		go func() {
			_ = getInstance()
		}()
	}
	time.Sleep(time.Second)
}
